package com.ruicar.afs.cloud.product.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.product.condition.ProductDiscountSubsidyCondition;
import com.ruicar.afs.cloud.product.entity.ProductDiscountSubsidy;
import com.ruicar.afs.cloud.product.mapper.ProductDiscountConfigMapper;
import com.ruicar.afs.cloud.product.service.ProductDiscountConfigService;
import com.ruicar.afs.cloud.product.vo.ExtrasInfoVo;
import com.ruicar.afs.cloud.product.vo.ProductDiscountSubsidyVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xudongdong
 * @date 2020-05-16
 */
@Service
public class ProductDiscountConfigServiceImpl extends ServiceImpl<ProductDiscountConfigMapper, ProductDiscountSubsidy> implements ProductDiscountConfigService {

    @Override
    public List<ExtrasInfoVo> getSelectDiscountRuleData() {
        return baseMapper.getSelectDiscountRuleData();
    }

    @Override
    public IPage<List<ProductDiscountSubsidyVo>> getDiscountConfigData(Page page, ProductDiscountSubsidyCondition config) {
        return baseMapper.getDiscountConfigData(page, config);
    }

    //复制方案
    @Override
    public void copyPlan(Long oldPlanID, Long newPlanID) {
        List<ProductDiscountSubsidy> list = this.list(Wrappers.<ProductDiscountSubsidy>query().lambda()
                .eq(ProductDiscountSubsidy::getDiscountPlanId, oldPlanID)
                .orderByAsc(ProductDiscountSubsidy::getDiscountSerialNumber)
        );
        for (ProductDiscountSubsidy productDiscountSubsidy : list) {
            this.copyOne(productDiscountSubsidy, newPlanID);
        }
    }

    //复制单个
    public void copyOne(ProductDiscountSubsidy productDiscountSubsidy, Long newPlanID) {
        productDiscountSubsidy.setId(null);
        productDiscountSubsidy.setCreateBy(null);
        productDiscountSubsidy.setCreateTime(null);
        productDiscountSubsidy.setDiscountPlanId(newPlanID);
        this.save(productDiscountSubsidy);
    }


}
